package com.etcn.manager.service.impl;

import javax.inject.Inject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.etcn.manager.model.TUser;
import com.etcn.manager.repository.UserRepository;
import com.etcn.manager.service.UserManagerService;

@Service("userManagerService")
public class UserManagerServiceImpl implements UserManagerService{
	@Inject
	private UserRepository userDao;
	
	@Override
	public TUser checkUser(String userName,String passWord){
		TUser user = getUserByUserName(userName);
		if(user!=null){
			if("admin".equals(user.getUserType())&& user.getPassword().equals(passWord)){
				return user;
			}
		}
		return null;
	}
	
	@Override
	public TUser checkDevUser(String userName, String passWord) {
		TUser user = getUserByUserName(userName);
		if(user!=null){
			if("developer".equals(user.getUserType())&& user.getPassword().equals(passWord)){
				return user;
			}else{
				return null;
			}
		}
		return null;
	}

	@Override
	public  TUser getUserByUserName(String userName){
		return userDao.findByUsername(userName);
	}
	@Override
	public boolean save(TUser user) {
		userDao.save(user);
		return true;
	}
	@Override
	public boolean update(TUser user) {
		userDao.save(user);
		return true;
	}
	@Override
	public boolean checkUsernameExist(String userName) {
		TUser user = userDao.findByUsername(userName);
		if(user!=null){
			return true;
		}else
			return false;
	}
	@Override
	public Page<TUser> getUsersPageable(String userType,Pageable pageable) {
		
		return userDao.getUsersPageable(userType,pageable);
	}
}
